package com.sitech.portal.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class CommUtil {
	
	//CALENDAR_COLUMNS与EVENT_PARAMS字段映射
    public static final List<String> CALENDAR_COLUMNS = Arrays.asList("ID", "TITLE", "ALLDAY", "NOTICE", "DESCRIPTION", "START_TIME", "END_TIME", "LOGIN_ID");
    public static final List<String> EVENT_PARAMS = Arrays.asList("id", "title", "allDay", "notice", "description", "start", "end", "login_id");
    public static final String USER_ADMIN = "admin";

    
	public static String[] splitToSS(String str, String sp) {
		ArrayList al = split(str, sp);

		Object[] os = al.toArray();
		String[] ss = new String[os.length];
		for (int i = 0; i < os.length; i++) {
			ss[i] = (String) os[i];
		}
		return ss;
	}

	public static ArrayList split(String str, String sp) {
		int ip = 0;
		ArrayList al = new ArrayList();
		ip = str.indexOf(sp);
		if (ip != -1) {
			// System.out.println(str.substring(0,ip));
			al.add(str.substring(0, ip));
			str = str.substring(ip + sp.length());
			// System.out.println(str);
			if (str.length() != 0) {
				al.addAll(split(str, sp));
			}
		} else {
			al.add(str);
		}
		return al;
	}

	public static String getBetweenStr(String s1, String s2) {

		if ((s1 == null) || (s1.trim().equals("")))
			return s1;
		if ((s2 == null) || (s2.trim().equals("")))
			return " = '" + s1 + "'";
		if (s1.compareTo(s2) > 0)
			return null;
		return " between '" + s1 + "' and '" + s2 + "' ";
	}

	public static String delCharFromString(String s, char c) {
		String rs = "";
		if (s == null)
			return null;
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) != c) {
				rs = rs + s.charAt(i);
			}
		}
		return rs;
	}

	// 计算 某天的所在一周的 周一的日期 day_of_week 格式 yyyymmdd
	// 返回格式 String pattern yyyy-MM-dd HH:mm:ss 或者 yyyyMMdd
	public String firstDayofWeek(String day_of_week, String pattern) {
		if (day_of_week.equals(""))
			return "";
		int dayofweek = 0;
		// String pattern = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		GregorianCalendar cal4 = new GregorianCalendar();
		cal4.set(Calendar.YEAR, Integer.parseInt(day_of_week.substring(0, 4)));
		cal4.set(Calendar.MONTH,
				Integer.parseInt(day_of_week.substring(4, 6)) - 1);
		cal4.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day_of_week.substring(
				6, 8)));
		Date date4 = cal4.getTime();

		if (cal4.get(Calendar.DAY_OF_WEEK) == 1) {
			dayofweek = 7;
		} else {
			dayofweek = cal4.get(Calendar.DAY_OF_WEEK) - 1;
		}

		GregorianCalendar cal_before = new GregorianCalendar();
		cal_before.set(Calendar.YEAR, Integer.parseInt(day_of_week.substring(0,
				4)));
		cal_before.set(Calendar.MONTH, Integer.parseInt(day_of_week.substring(
				4, 6)) - 1);
		cal_before.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day_of_week
				.substring(6, 8)));
		cal_before.add(Calendar.DATE, 1 - dayofweek);
		Date date_before = cal_before.getTime();
		String first_Day_Week = dateFormat.format(date_before);

		return first_Day_Week;
	}

	// 计算 某天的所在一周的 周七的日期 day_of_week 格式 yyyymmdd
	// 返回格式 String pattern yyyy-MM-dd HH:mm:ss 或者 yyyyMMdd
	public String lastDayofWeek(String day_of_week, String pattern) {
		if (day_of_week.equals(""))
			return "";
		int dayofweek = 0;
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		GregorianCalendar cal4 = new GregorianCalendar();
		cal4.set(Calendar.YEAR, Integer.parseInt(day_of_week.substring(0, 4)));
		cal4.set(Calendar.MONTH,
				Integer.parseInt(day_of_week.substring(4, 6)) - 1);
		cal4.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day_of_week.substring(
				6, 8)));
		Date date4 = cal4.getTime();

		if (cal4.get(Calendar.DAY_OF_WEEK) == 1) {
			dayofweek = 7;
		} else {
			dayofweek = cal4.get(Calendar.DAY_OF_WEEK) - 1;
		}

		GregorianCalendar cal_before = new GregorianCalendar();
		cal_before.set(Calendar.YEAR, Integer.parseInt(day_of_week.substring(0,
				4)));
		cal_before.set(Calendar.MONTH, Integer.parseInt(day_of_week.substring(
				4, 6)) - 1);
		cal_before.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day_of_week
				.substring(6, 8)));
		cal_before.add(Calendar.DATE, 7 - dayofweek);
		Date date_before = cal_before.getTime();
		String last_Day_Week = dateFormat.format(date_before);

		return last_Day_Week;
	}

	// 计算 某天的所在月的 月头的日期 day_of_week 格式 yyyymmdd
	public String firstDayofMonth(String day_of_month) {
		if (day_of_month.equals(""))
			return "";
		String first_Day_Month = "";

		first_Day_Month = day_of_month.substring(0, 4)
				+ day_of_month.substring(4, 6) + "01";

		return first_Day_Month;
	}

	// 计算 某天的所在月的 月头的日期 day_of_week 格式 yyyymmdd
	public String lastDayofMonth(String day_of_month, String pattern) {
		if (day_of_month.equals(""))
			return "";

		String last_Day_Month = "";
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		GregorianCalendar cal4 = new GregorianCalendar();
		cal4.set(Calendar.YEAR, Integer.parseInt(day_of_month.substring(0, 4)));
		cal4.set(Calendar.MONTH,
				Integer.parseInt(day_of_month.substring(4, 6)) - 1);
		cal4.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day_of_month
				.substring(6, 8)));
		Date date4 = cal4.getTime();
		last_Day_Month = dateFormat.format(date4);

		GregorianCalendar cal_last = new GregorianCalendar();
		cal_last.set(Calendar.YEAR, Integer.parseInt(day_of_month.substring(0,
				4)));
		cal_last.set(Calendar.MONTH, Integer.parseInt(day_of_month.substring(4,
				6)) - 1);
		cal_last.set(Calendar.DAY_OF_MONTH, cal4
				.getActualMaximum(Calendar.DAY_OF_MONTH));
		Date date_last = cal_last.getTime();
		last_Day_Month = dateFormat.format(date_last);

		return last_Day_Month;
	}

	// 获取 两个时间之间的差值 String beginDate,String endDate 时间格式 yyyymmdd
	public int diffTime(String beginDate, String endDate) {
		int dayDiff = 0;
		DateFormat mFormat1 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		Date start_Date = null;
		Date end_Date = null;
		try {
			start_Date = mFormat1.parse(beginDate.trim());
			end_Date = mFormat1.parse(endDate.trim());
			dayDiff = (int) ((end_Date.getTime() - start_Date.getTime()) / (24 * 60 * 60 * 1000));
			// System.out.println("true "+ dayDiff );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dayDiff;
	}

	// 获取 下一个时间点
	/*
	 * @param beginDate 开始时间 格式 "yyyyMMdd HH:mm:ss" @param dayNum 增加的天数 @param
	 * strFormat 返回的格式 java 的格式 "yyyyMMdd HH:mm:ss" "yyyyMMdd" "yyyy-MM-dd"
	 * @return String
	 */
	public String addDateTime(String beginDate, int dayNum, String strFormat) {
		Calendar cal = Calendar.getInstance();
		DateFormat mFormat1 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat(strFormat);
		Date start_Date = null;
		try {
			start_Date = mFormat1.parse(beginDate.trim());
			cal.setTime(start_Date);
			cal.add(Calendar.DATE, dayNum);
			// System.out.println("true "+ df.format(cal.getTime()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return df.format(cal.getTime());
	}

	public CommUtil() {
	}

	public static void main(String[] args) {
		CommUtil commUtil1 = new CommUtil();
		String ANALY_TIME = "20080121";
		// System.out.println("first day of week
		// "+commUtil1.firstDayofWeek(ANALY_TIME,"yyyyMMdd"));
		// System.out.println("last day of week
		// "+commUtil1.lastDayofWeek(ANALY_TIME,"yyyyMMdd"));
		// System.out.println("last day of month
		// "+commUtil1.lastDayofMonth(ANALY_TIME,"yyyyMMdd"));
		System.out.println("last day of month "
				+ commUtil1.addDateTime(ANALY_TIME + " 13:00:00", 22,
						"yyyy-MM-dd HH:mm:ss"));
		
		String password = org.apache.commons.codec.binary.Hex.encodeHexString("bsm3".getBytes());
		System.out.println(password);
	}
}